package com.atguigu.juc;

import java.util.concurrent.*;

/**
 * @author: 封捷
 * @create-date: 2022/8/29 10:36
 */
public class DemoCompletableFuture02 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        // 1、创建自定义线程池
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                3,
                5,
                10,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(5));

        // 2、封装两个连续执行的任务
        String finalTaskResult = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + " 第一个任务");
            return "first task result";
        }, executor).thenApplyAsync((String prevTaskResult) -> {
            System.out.println(Thread.currentThread().getName() + " 第二个任务");
            System.out.println("前面任务的返回值 = " + prevTaskResult);
            return "second task result";
        }).get();

        System.out.println("所有任务的最终返回值 = " + finalTaskResult);

    }

}
